#include <gtk/gtk.h>

/*
 * Externs
 */
extern void show_sys_tray_icon();


/*
 * Function		: toggle_button_callback
 * Description	: Invokes the service trigger code for related sync-up process
 */
static void
toggle_button_callback(GtkWidget *widget, gpointer window)
{
	if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget))) {
		// TODO: Button is set, trigger the related service
	} else {
		// TODO: Button is not set, cancel service
	}
}


/*
 * Function		: delete_event
 * Description	: Invokes method to quit gtk window when close button is clicked
 */
static gboolean
delete_event(GtkWidget *widget, GdkEvent *event, gpointer data)
{
	gtk_main_quit();
	return FALSE;
}


/*
 * Function		: destroy
 * Description	: Invokes method to quit gtk window when close button is clicked
 */
static void
destroy(GtkWidget *widget, gpointer data)
{
	gtk_main_quit();
}


/*
 * Function		: show_help
 * Description	: Shows the help window for sync-up program
 */
static void
show_help(GtkWidget *widget, gpointer window)
{
	GtkWidget *dialog;
	
	dialog = gtk_message_dialog_new(window,
		GTK_DIALOG_DESTROY_WITH_PARENT,
		GTK_MESSAGE_INFO,
		GTK_BUTTONS_CLOSE,
		"Sync-up helps you to sync your application data over different \
operating systems installed in your computer. In order to sync the \
data of an application simply tick its checkbox. If you want \
to stop sync-ing data simply untick the box.");
	gtk_window_set_title(GTK_WINDOW(dialog), "Sync-up Help");
	gtk_dialog_run(GTK_DIALOG(dialog));
	gtk_widget_destroy(dialog);
}



/*
 * Main driver function
 */
int 
main(int argc, char* argv[])
{
	GtkWidget *window;
	GtkWidget *table;
	
	GtkWidget *check_thunderbird;
	GtkWidget *check_firefox;
	GtkWidget *button_help;
	GtkWidget *button_close;
	
	
	
	gtk_init(&argc, &argv);
	
	// instantiate parent window
	window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_window_set_title(GTK_WINDOW(window), "Sync-up Preferences");
	gtk_window_set_default_size(GTK_WINDOW(window), 300, 250);
	gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
	gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
	
	// create 4 rows, 2 cols table
	table = gtk_table_new(4, 2, FALSE);
	gtk_table_set_col_spacings(GTK_TABLE(table), 2);

	// create check button for thunderbird	
	check_thunderbird = gtk_check_button_new_with_label("Thunderbird");
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check_thunderbird), FALSE);
	gtk_table_attach(GTK_TABLE(table), check_thunderbird, 0, 1, 1, 2, 
		GTK_FILL, GTK_SHRINK, 1, 1);

	// create check button for firefox	
	check_firefox = gtk_check_button_new_with_label("Firefox");
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check_firefox), FALSE);
	gtk_table_attach(GTK_TABLE(table), check_firefox, 0, 1, 2, 3, 
		GTK_FILL, GTK_FILL | GTK_EXPAND, 1, 1);
	
	// create a help button
  	button_help = gtk_button_new_with_label("Help");
  	gtk_widget_set_size_request(button_help, 70, 30);
  	gtk_table_attach(GTK_TABLE(table), button_help, 0, 1, 3, 4, 
  		GTK_FILL, GTK_FILL, 0, 0);
  		
  	// create a close button
	button_close = gtk_button_new_with_label("Close");
	gtk_widget_set_size_request(button_close, 70, 30);
	gtk_table_attach(GTK_TABLE(table), button_close, 1, 2, 3, 4, 
		GTK_FILL, GTK_FILL, 0, 0);

	// attach table to main window
	gtk_container_add(GTK_CONTAINER(window), table);
	
	// connect delete_event to main window
	g_signal_connect(window, "delete_event",
		G_CALLBACK(delete_event), NULL);
	
	// connect destroy callback to main window
	g_signal_connect(window, "destroy",
		G_CALLBACK(destroy), NULL);
		
	g_signal_connect(button_close, "clicked",
		G_CALLBACK(delete_event), NULL);
	
	g_signal_connect(G_OBJECT(check_thunderbird), "clicked",
		G_CALLBACK(toggle_button_callback), NULL);
		
	g_signal_connect(G_OBJECT(check_firefox), "clicked",
		G_CALLBACK(toggle_button_callback), NULL);
		
	g_signal_connect(G_OBJECT(button_help), "clicked", 
		G_CALLBACK(show_help), (gpointer) window);
	
	// init a system tray icon for sync-up process
	show_sys_tray_icon();
	
	gtk_widget_show_all(window);
	
	gtk_main();
	
	return 0;
}
